<?php

namespace App\Models\User;

use App\Libraries\Tools\ConstVar;
use App\Libraries\Tools\Msg;
use App\Models\BaseModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class Balance extends BaseModel
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'updated_at',
    ];

    protected $table = 'balance_log';
    protected $dateFormat = 'U';


    /**
     * @param $reason
     * @param $from_id
     * @param $to_id
     * @param $from_channel
     * @param $to_channel
     * @param $origin_type
     * @param $origin_id
     * @param $balance
     * @param $purchase_id
     * @param int $hong_bao_id
     * @param int $hong_bao_amount
     * @param int $status
     * @param int $from_type
     * @param int $to_type
     * @param int $refund
     * @return mixed
     */
    public function add($reason, $from_id, $to_id, $from_channel, $to_channel, $origin_type,
                        $origin_id, $balance, $purchase_id, $hong_bao_id=0, $hong_bao_amount=0,
                        $status=0, $from_type=1, $to_type=1, $refund=0){
        $balance_log = new Balance();
        $balance_log->reason = $reason;
        $balance_log->from_id = $from_id;
        $balance_log->to_id = $to_id;
        $balance_log->from_channel = $from_channel;
        $balance_log->to_channel = $to_channel;
        $balance_log->origin_type = $origin_type;
        $balance_log->origin_id = $origin_id;
        $balance_log->balance = $balance;
        $balance_log->purchase_id = $purchase_id;
        $balance_log->hong_bao_id = $hong_bao_id;
        $balance_log->hong_bao_amount = $hong_bao_amount;
        $balance_log->status = $status;
        $balance_log->from_type = $from_type;
        $balance_log->to_type = $to_type;
        $balance_log->refund = $refund;
        $balance_log->save();
        return $balance_log->id;
    }

    /**
     * @param Request $request
     * @param $user
     * @param $model
     * @param null $model_ext
     * @return array
     */
    public function balanceLogic(Request $request, $user, $model, $model_ext=null){
        $rs = $model->beforeCheck($request, $model_ext);
        if($rs['code'] != 200){
            $this->rs_format['code'] = $rs['code'];
            return $this->rs_format;
        }
        $real_amount = $rs['data']['price'] - ($rs['data']['hong_bao_amount']??0);
        $flag = $user->where('user_id', $user->user_id)
            ->where('balance', '>=', $real_amount)
            ->decrement('balance', $real_amount);
        if(!$flag){
            $this->rs_format['code'] = 40004;
            return $this->rs_format;
        }
        $this->rs_format['data']['ext_id'] = $rs['data']['ext_id'];
        $this->rs_format['data']['price'] = $rs['data']['price'];
        $this->rs_format['data']['hong_bao_id'] = $rs['data']['hong_bao_id']??0;
        $this->rs_format['data']['hong_bao_amount'] = $rs['data']['hong_bao_amount']??0;
        return $this->rs_format;
    }

    /**
     * 获取余额流水
     * @param $id
     * @param $page
     * @param $size
     * @param $type 0-系统 1-用户 2-商家
     * @param $start_at
     * @param $end_at
     * @return mixed
     */
    public function getBalances($id, $page, $size, $type, $start_at, $end_at){
        $data['list'] = $this->whereRaw("(`from_id` = {$id} AND `from_type` = {$type} OR (`to_id` = {$id} AND `status` = 1 AND `to_type` = {$type}))")
            ->whereBetween('created_at', [$start_at, $end_at])->forPage($page, $size+1)
            ->orderBy('created_at', 'DESC')->get();
        $data['next_page'] = $page + 1;
        $data['page_size'] = $size;
        return $this->after($data);
    }
}
